#define _POSIX_C_SOURCE 199309L 

#include "uart.h"

struct rk_serial serial_info;
struct timeval start, end;

void* first_serial_422_read_process_thread(void* arg)
{
    while (1) {
        if (serial_info.Read_Flag) {
            memset(serial_info.buffer_serial1, 0, sizeof(serial_info.buffer_serial1));

            ssize_t totalBytesRead = 0;
            while (totalBytesRead < 319) {
                serial_info.bytesRead = read(serial_info.serial_fd1, 
                                         serial_info.buffer_serial1 + totalBytesRead, 
                                         319 - totalBytesRead);

                if (serial_info.bytesRead > 0) {
                    totalBytesRead += serial_info.bytesRead;
                } else if (serial_info.bytesRead == 0) {
                    // break;
                    // continue;
                } else {
                    perror("read error");
                    break;
                }
            }
            
            if (totalBytesRead == 319) {
                serial_info.Write_Flag = 1;

                gettimeofday(&end, NULL);  // 记录结束时间
                long read_time = (end.tv_sec - start.tv_sec) * 1000000 + (end.tv_usec - start.tv_usec); // 微秒为单位
                printf("Read time: %ld microseconds\n", read_time);  // 打印读取数据的时间

                printf("Data received (%zd bytes): ", totalBytesRead);
                for (ssize_t i = 0; i < totalBytesRead; ++i) {
                    printf("%c", serial_info.buffer_serial1[i]);
                }
                printf("\n");
                fflush(stdout);
            } else {
                printf("Incomplete data received (%zd bytes): ", totalBytesRead);
                for (ssize_t i = 0; i < totalBytesRead; ++i) {
                    printf("%c", serial_info.buffer_serial1[i]);
                }
                printf("\n");
                fflush(stdout);
            }
        }
    }
}


void* second_serial_422_read_process_thread(void* arg)
{
    int a = 1;
    while (1) {
        if (serial_info.Write_Flag) {
            
            sprintf(serial_info.buffer_serial2, 
                "\n%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n"
                "%d, %.3f, %.3f, %.3f, %.3f, %.3f, %.3f\n, %.3f\t\n",
                a, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                2, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                3, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                4, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                5, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                6, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                7, 0.123, 0.124, 0.125, 0.126, 0.127, 0.128,
                0.789);
            a++;
            if (a == 9){
                a = 1;
            }

            size_t totalBytes = strlen(serial_info.buffer_serial2) + 1; 
            size_t sentBytes = 0; 
            
            gettimeofday(&start, NULL); // 记录开始时间

            while (sentBytes < totalBytes) {
                
                serial_info.bytesWrite = write(serial_info.serial_fd2, 
                                           serial_info.buffer_serial2 + sentBytes, 
                                           totalBytes - sentBytes);

                if (serial_info.bytesWrite > 0) {
                    sentBytes += serial_info.bytesWrite; 

                    // Print successfully sent data
                    // for (ssize_t i = 0; i < bytesWrite; ++i) {
                    //     printf("%c", serial_info.buffer_serial2[sentBytes - bytesWrite + i]);
                    // }
                    // fflush(stdout);
                } else {
                    perror("write error"); 
                    break; 
                }
            }

            if (sentBytes == totalBytes) {
                // printf("\nAll data sent successfully: %zu bytes\n", sentBytes);
            } else {
                printf("\nData sending incomplete: %zu/%zu bytes sent\n", sentBytes, totalBytes);
            }

            memset(serial_info.buffer_serial2, 0, sizeof(serial_info.buffer_serial2)); 
            serial_info.Write_Flag = 0; 
        }
    }
}



void timer_handler(int sig, siginfo_t *si, void *uc) { 
	timer_t *tidp; 
	tidp = si->si_value.sival_ptr;  

	if (*tidp == timers[0]) {
		// printf("Timer %d expired\n", 1); 
	} else if (*tidp == timers[1]) {
		// printf("Timer %d expired\n", 2); 
	} else if (*tidp == timers[2]) {
		// printf("Timer %d expired\n", 3); 
	} else if (*tidp == timers[3]) {
		// printf("Timer %d expired\n", 4); 
	} 
} 

int main() {
    
    serial_info.Read_Flag = 1;
    serial_info.Write_Flag = 1;

    // 串口1和串口2参数设置函数
    if(init_serial() == -1) {
        return -1;
    }

	if (create_timers() == -1) {
        exit(EXIT_FAILURE);
    }

    int ret;

    pthread_t first_serial_422;
	ret = pthread_create(&first_serial_422, NULL, first_serial_422_read_process_thread, NULL);
	if (ret != 0) {
		printf("first serial 422 create fial\n");
		return -1;
	}
	pthread_detach(first_serial_422);

	pthread_t second_serial_422;
	ret = pthread_create(&second_serial_422, NULL, second_serial_422_read_process_thread, NULL);
	if (ret != 0) {
		printf("second serial 422 create fial\n");
		return -1;
	}
	pthread_detach(second_serial_422);
    
    while (1) {
        sleep(1);
    }
	return 0; 
}